﻿package com.atn.edamame.googlews 
{
	import flash.events.*;
	import flash.net.*; 
	import com.atn.edamame.*;
	import com.atn.edamame.tools.*;
	import flash.text.*;
	import org.flashdevelop.utils.FlashConnect;
	import mx.core.UIComponent;
	public class WebImageSearch extends EdamameControl
	{
		private var searchResult:Array = new Array();
		private var waitIndex:int = 0; 
		private var searchCache:Object;
		private var funcName:String = "googleservice.imagessearch";
		public override function init():void {
				addEventListener(Event.ENTER_FRAME, enterFrameHandler);
				searchCache = new Object();
				super.init();
		}
		
		protected function enterFrameHandler(event:Event):void
		{
			waitIndex++;
			if ( waitIndex > 50 && edamameParent.drag == false && searchResult.length > 1)
			{
				
				var search:Object = searchResult.shift();
				
				if(search != null){
					var o:EdamameImage = new EdamameImage();
					edamameParent.addEdamameObject(o);
					o.x = this.x;
					o.y = this.y + this.height;
					o.vy = 2;
					o.value = search.url;
					waitIndex = 0;
				}
			}
		}
		
		protected function onComplete(results:Object) :void {
				var waitIndex:int = 0; 
				for each(var obj:Object in results.responseData.results) {
					searchResult.push(obj);
				}
		}
		
		protected function onFail(results:Object) :void{
		}
		
		public override function collide(b: EdamameObject):void
		{
			if (b is EdamameString) {
				var c:EdamameString = b as EdamameString;
				var onComplete:Function = function onComplete(results:Object) :void {
							var waitIndex:int = 0; 
							searchCache[c.value] = results.responseData.results;
							for each(var obj:Object in results.responseData.results) {
								searchResult.push(obj);
							}
				}
				if(searchCache[c.value])
				{
					if (searchResult.length < 5)
					{
							for each(var obj:Object in searchCache[c.value]) {
								searchResult.push(obj);
							}
					}
				}else{	
					var responder:Responder = new Responder(onComplete, onFail);
					GConnection.getInstance().getConnection().call("googleservice.imagessearch", responder, c.value);
				}
				b.destroy();
			}
		}
		
		
		public override function destroy():void
		{
			removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
			super.destroy();
		}
		
		public override function exec(v:Object, onFinish:Function):void
		{
			/*
			v.value = new Array();
			for (var i:int = 0 ; i < 10 ; i++ ) {
				v.value.push({"title":"test","imageId":"imageID","content":"test","url":"http://localhost/asset/img04.jpg","originalContextUrl":"http://localhost/"});
			}
			onFinish( { result:"ok" } );
			return ;
			*/
			var responder:Responder = new Responder(function (results:Object):void
			{
				v.value = new Array();
				if (results == null)
				{
					onFinish( { result:"ng" } );
					return ;
				}				
				for each(var obj:Object in results.responseData.results) {
							v.value.push(obj);
				}
				onFinish( { result:"ok" } );
			}, onFail);
			
			GConnection.getInstance().getConnection().call(funcName, responder,v.value[0]);
		}
	}
	
}